package com.jerehedu.myapplication34.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jerehedu.myapplication34.R;
import com.jerehedu.myapplication34.adapter.PrizeGridAdapter;
import com.jerehedu.myapplication34.entrty.Prize;
import com.jerehedu.myapplication34.util.ThreadPoolManager;
import com.jerehedu.myapplication34.view.UnScrollGridView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ConversionActivity extends AppCompatActivity {
    private List<Prize> mData=new ArrayList<>();
    private PrizeGridAdapter adapter;
    private UnScrollGridView gridView;
    private CircleImageView centerHead;
    private TextView num,score;
    private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversion);
        adapter=new PrizeGridAdapter(ConversionActivity.this,mData);
        gridView=(UnScrollGridView)findViewById(R.id.gridView);
        gridView.setAdapter(adapter);
        centerHead=(CircleImageView)findViewById(R.id.centerHead);
        num=(TextView)findViewById(R.id.num);
        score=(TextView)findViewById(R.id.score);
        back=(ImageView)findViewById(R.id.back);
        centerHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否登录过

            }
        });
        ThreadPoolManager.getInstance().getExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                /*OkHttpUtils.get()
                        .url()
                        .addParams()*/
                SharedPreferences sp = getSharedPreferences("SPIsLogin", Context.MODE_PRIVATE);
                String s = sp.getString("userImg", null);
                ArrayList<Prize> list = new ArrayList<Prize>();
                list.add(new Prize(s, "苹果", 600));
                list.add(new Prize(s, "苹果", 500));
                list.add(new Prize(s, "苹果", 500));
                list.add(new Prize(s, "苹果", 600));
                list.add(new Prize(s, "苹果", 600));
                list.add(new Prize(s, "苹果", 600));
                Message msg = handler.obtainMessage(1);
                msg.obj = list;
                handler.sendMessage(msg);

            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Prize prize = mData.get(position);
                int score = prize.getScore();
                //与服务器进行交互
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                ArrayList<Prize> list=(ArrayList<Prize>)msg.obj;
                mData.addAll(list);
                adapter.notifyDataSetChanged();
            }
        }
    };


}
